#include <string.h>
#include "timer.h"

tmr_t tmrInit(void)
{
  tmr_t timer;
  
  memset(&timer, 0, sizeof(timer));

  return timer;
}

void tmrStart(tmr_t *timer)
{
  gettimeofday(&timer->started, NULL);
}

void tmrStop(tmr_t *timer)
{
  gettimeofday(&timer->stopped, NULL);
}

unsigned long long tmrGetValueInUs(const tmr_t *timer)
{
  time_t sec;
  suseconds_t usec;

  sec = timer->stopped.tv_sec - timer->started.tv_sec;
  usec = timer->stopped.tv_usec - timer->started.tv_usec;

  return (unsigned long long)(sec*1000000)+usec;
}

void tmrSetValueInUs(tmr_t *timer, unsigned long long int value)
{
  timer->started.tv_sec = timer->started.tv_usec = 0;
  timer->stopped.tv_sec = value/1000000;
  timer->stopped.tv_usec = value%1000000;
}
